package com.eralink.unionuhfreader.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.eralink.unionuhfreader.R;
import com.eralink.unionuhfreader.bean.ReaderBean;

import java.util.List;

public class ReaderListViewAdapter extends BaseAdapter {
    private final List<ReaderBean> finderReportReaderBeanList;
    private final LayoutInflater layoutInflater;

    private int selectedItemId = -1;

    public ReaderListViewAdapter(Context context, List<ReaderBean> finderReportReaderBeanList) {
        this.layoutInflater = LayoutInflater.from(context);
        this.finderReportReaderBeanList = finderReportReaderBeanList;
    }

    public void setSelectedItemId(int selectedItemId) {
        if (0 > selectedItemId || selectedItemId >= finderReportReaderBeanList.size()) {
            this.selectedItemId = -1;
        } else {
            this.selectedItemId = selectedItemId;
        }
    }

    public int getSelectedItemId() {
        return selectedItemId;
    }

    @Override
    public int getCount() {
        return finderReportReaderBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return finderReportReaderBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        TagListView tagListView;
        if (view == null){
            view = layoutInflater .inflate(R.layout.reader_list_item,parent,false);
            tagListView = new TagListView();
            tagListView.tvReaderId = view.findViewById(R.id.tv_reader_id);
            tagListView.tvReaderIp = view.findViewById(R.id.tv_reader_ip);
            tagListView.tvReaderSn = view.findViewById(R.id.tv_reader_sn);
            tagListView.tvReaderName = view.findViewById(R.id.tv_reader_name);
            view.setTag(tagListView);
        }
        else {
            tagListView = (TagListView) view.getTag();
        }

        tagListView.tvReaderId.setText(String.valueOf(finderReportReaderBeanList.get(position).getReaderId()));
        tagListView.tvReaderIp.setText(finderReportReaderBeanList.get(position).getIp());
        tagListView.tvReaderSn.setText(finderReportReaderBeanList.get(position).getSn());
        tagListView.tvReaderName.setText(finderReportReaderBeanList.get(position).getName());

        tagListView.tvReaderId.setTextColor(selectedItemId == position ? Color.BLUE : Color.BLACK);
        tagListView.tvReaderIp.setTextColor(selectedItemId == position ? Color.BLUE : Color.BLACK);
        tagListView.tvReaderSn.setTextColor(selectedItemId == position ? Color.BLUE : Color.BLACK);
        tagListView.tvReaderName.setTextColor(selectedItemId == position ? Color.BLUE : Color.BLACK);

        return view;
    }

    static class TagListView {
        public TextView tvReaderId;
        public TextView tvReaderIp;
        public TextView tvReaderSn;
        public TextView tvReaderName;
    }
}
